package com.xinmao.quantitative.trad.strategies.combine;

import com.xinmao.quantitative.trad.strategies.IStrategy;
import org.springframework.stereotype.Component;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseStrategy;
import org.ta4j.core.Rule;
import org.ta4j.core.Strategy;
import org.ta4j.core.indicators.EMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.Num;
import org.ta4j.core.rules.CrossedDownIndicatorRule;
import org.ta4j.core.rules.OverIndicatorRule;
import org.ta4j.core.rules.UnderIndicatorRule;

/**
 * decision point trend model Strategy
 *
 * @see <a href="https://school.stockcharts.com/doku.php?id=trading_strategies:decisionpoint_trend_model">
 *      https://school.stockcharts.com/doku.php?id=trading_strategies:decisionpoint_trend_model</a>
 */
@Component
public class DecisionPointStrategy implements IStrategy {



    @Override
    public  Strategy buildStrategy(BarSeries series) {

        ClosePriceIndicator closePrice = new ClosePriceIndicator(series);

        // Long-Term Trend :50-200(Day) 17-34(Week) 6-10(Month)
        EMAIndicator ema200 = new EMAIndicator(closePrice, 200);
        EMAIndicator ema50 = new EMAIndicator(closePrice, 50);

        //Short-Term Trend
        EMAIndicator ema20 = new EMAIndicator(closePrice, 20);
        EMAIndicator ema5 = new EMAIndicator(closePrice, 5);

        // Entry rule
        Rule entryRule = new OverIndicatorRule(ema20, ema50).and(new OverIndicatorRule(ema50, ema200))
                .and(new CrossedDownIndicatorRule(ema5, ema20));

        // Exit rule
        Rule exitRule = new UnderIndicatorRule(ema5, ema20).and(new UnderIndicatorRule(ema20, ema50));

        return new BaseStrategy(entryRule, exitRule);
    }

    @Override
    public Num getScore() {
        return DecimalNum.valueOf(1);
    }
}
